var App = {

    login: function(callback) {
        // // tmp
        // if (WStorage.getItem('userInfo')) {
        //     callback(true);
        //     $('.m-logout').click(function() {
        //         App.logout();
        //     });
        //     return;
        // }
        // location.href = '/bs/index.html';
        // prod
        Apis.islogin(function(result) {
            if (result) {

                var userInfo = App.getUserInfo();
                if (userInfo.role == 1) {
                    domainSwitchList(function() {
                        callback();
                    });
                } else {
                    domainSwitchList(function() {
                        callback();
                    });
                }

                $('.m-logout').click(function() {
                    App.logout();
                });

                // keeper session
                setInterval(function() {
                    Apis.keepsession();
                }, 1000 * 60 * 5);

                return;
            }
            WStorage.removeItem("mp-siteId");
            WStorage.removeItem("userInfo");
            location.href = '/support/tool/';
        });
    },

    dologin: function(par, callback) {
        Apis.login(par, function(result, userInfoOrMessage) {
            if (result) {
                var userInfo = userInfoOrMessage;
                WStorage.setItem("userInfo", userInfo);
                WStorage.setItem("mp-siteId", userInfo.siteId);
                callback(result, userInfo);
                return;
            }
            WStorage.removeItem("mp-siteId");
            WStorage.removeItem("userInfo");
            var message = userInfoOrMessage;
            callback(result, message);
        });
    },

    logout: function(callback) {
        // // tmp
        // WStorage.removeItem("userInfo");
        // location.href = '/bs/index.html';
        // callback && callback();
        // prod
        Apis.logout(function() {
            WStorage.removeItem("mp-siteId");
            WStorage.removeItem("userInfo");
            location.href = '/support/tool/';
            callback && callback();
        });
    },

    setBookMode: function(bookMode) {
        WStorage.setItem('mp-bookMode', bookMode);
    },

    getBookMode: function() {
        var bookMode = WStorage.getItem('mp-bookMode');
        return bookMode ? bookMode : '';
    },

    setSiteId: function(siteId) {
        WStorage.setItem('mp-siteId', siteId);
    },

    getSiteId: function() {
        var siteId = WStorage.getItem('mp-siteId');
        return siteId ? siteId : 0;
    },

    initKeepSession: function() {
        setInterval(function() {
            Apis.keepsession();
        }, 60 * 1000);
    },

    initLang: function() {
        var eLanguageTv = $('.mp-language .mp-language-tv');
        var eLanguageMenu = $('.mp-language .dropdown-menu');
        var wLanguage = WStorage.getItem('mp-language');
        var viewLanguage = function(wLanguage) {
            if (wLanguage) {
                eLanguageTv.empty()
                    .append('当前版本：')
                    .append((function() {
                        if (wLanguage == 'zhCN')
                            return '简体中文';
                        if (wLanguage == 'jaJP')
                            return '日本語';
                        if (wLanguage == 'enUS')
                            return 'English';
                        if (wLanguage == 'zhTW')
                            return '繁體中文';
                        if (wLanguage == 'ko')
                            return '한국어';
                        if (wLanguage == 'th')
                            return 'ไทย';
                    })())
                    .append('<span class="caret"></span>')
                ;
            } else {
                eLanguageTv.empty()
                    .append('当前版本：')
                    .append('简体中文')
                    .append('<span class="caret"></span>')
                ;
                WStorage.setItem('mp-language', 'zhCN');
            }
        };
        viewLanguage(wLanguage);
        $('[data-lang]', eLanguageMenu).click(function() {
            var wLanguage = $(this).data('lang');
            WStorage.setItem('mp-language', wLanguage);
            viewLanguage(wLanguage);
            location.reload();
        });
    },

    initUserName: function() {
        $('.mp-login-user').html(App.getUserInfo().userName);
    },

    getLang: function() {
        var wLanguage = WStorage.getItem('mp-language');
        return wLanguage ? wLanguage : 'zhCN';
    },

    initSelect2: function(element) {
        $('.mp-select2', element).select2({
            minimumResultsForSearch: 20
        });
    },

    initSelect2Multiple: function(element) {
        $(".mp-select2-multiple", element).select2({
            minimumResultsForSearch: 20
        });
    },

    getUserInfo: function() {
        var userInfo = WStorage.getItem('userInfo');
        if (userInfo) {
            return userInfo;
        }
        return null;
    },

    init: {
        datepicker: function(rootElement) {
            $('input.datepicker', rootElement).datepicker({
                language: 'zh-CN',
                autoclose: true,
                format: 'yyyy-mm-dd'
            });
        }
    }
};

var SITE_DATA = [];
// 头部网站列表切换
function domainSwitchList(callback) {
    var condition = {};
    condition.lang = App.getLang();
    condition.siteId = 0;
    Apis.getDomains(condition, function(response) {
        var list = response.data ? response.data : [];
        if (!list || list.length == 0) {
            return;
        }
        SITE_DATA = list;
        var listContianer = $('.mp-domain .dropdown-menu');
        list.forEach(function(m) {
            var li = $('<li/>').appendTo(listContianer);
            $('<a href="javascript:;" data-domain=""/>')
                .attr('data-domain', m.id)
                .html(m.siteName)
                .click(function() {
                    var domain = $(this).data('domain');
                    App.setSiteId(domain);
                    location.reload();
                })
                .appendTo(li);
        });

        var userInfo = App.getUserInfo();
        if (userInfo.role == 1) {
            var id = App.getSiteId();
            if (id) {
                var site = SITE_DATA.filter(function(m) {
                    return m.id == id;
                })[0];
                if (site) {
                    $('.mp-domain-tv').html('当前网站：' + site.siteName + ' <span class="caret"></span>');
                    $('.mp-head-logo a').html(site.siteName);
                    $('.mp-menu-wrapper [data-parent-title]')
                        .attr('data-parent-title', site.siteName)
                        .attr('data-book-mode', 'mode_' + site.id)
                } else {
                    App.setSiteId(list[0].id);
                }
            } else {
                App.setSiteId(list[0].id);
            }
        } else {
            var id = userInfo.siteId;
            var site = SITE_DATA.filter(function(m) {
                return m.id == id;
            })[0];
            if (site) {
                $('.mp-domain-tv').html('当前网站：' + site.siteName + ' <span class="caret"></span>');
                $('.mp-head-logo a').html(site.siteName);
                $('.mp-menu-wrapper [data-parent-title]')
                    .attr('data-parent-title', site.siteName)
                    .attr('data-book-mode', 'mode_' + site.id)
            }
        }

        callback();
    });
}
